package main

import (
	"fmt"
	"sync"
)

// 全局变量
var num = 0

// 计数器锁
var wg sync.WaitGroup

// 互斥锁
var lock sync.Mutex

func foo() {
	defer wg.Done()
	lock.Lock()
	for i := 0; i < 5000; i++ {
		num += 1
	}
	lock.Unlock()
}

func bar() {
	defer wg.Done()
	lock.Lock()
	for i := 0; i < 5000; i++ {
		num += 1
	}
	lock.Unlock()
}

func main() {
	wg.Add(2)
	go foo()
	go bar()
	wg.Wait()
	fmt.Println(num)
}
